package mqtt

import (
	"sync"

	MQTT "github.com/eclipse/paho.mqtt.golang"
)

var cmdLock = new(sync.RWMutex)
var resLock = new(sync.RWMutex)

//CmdResponse 回应
type CmdResponse struct {
	CmdType  string `json:"type"`
	CmdID    int64  `json:"seq"`
	SourceID string `json:"sid"`
	ProError
	TimeStamp int64       `json:"timestamp"`
	Body      interface{} `json:"result"`
	Client    MQTT.Client `json:"-"`
}

//Cmd 命令格式
type Cmd struct {
	CmdType   string      `json:"type"`
	CmdID     int64       `json:"seq"`
	TimeStamp int64       `json:"timestamp"`
	Body      interface{} `json:"body"`
	SourceID  string      `json:"sid"`
}

type topicCmdInfo struct {
	topic string
	cmdID string
	//mqttAddr string
}

//var topics = make([]string, 0)
//var topicsReply = make([]string, 0)

//ProError 错误结构
type ProError struct {
	Code  int    `json:"errorCode"`
	Error string `json:"errorDiscribe"`
}

//FunCmdHandler 命令处理函数
type FunCmdHandler = func(client MQTT.Client, fromTopic string, cmd *Cmd) (interface{}, *ProError)

//FunCmdResponseHandler 命令返回处理函数
type FunCmdResponseHandler = func(client MQTT.Client, fromTopic string, isTimeout bool, res *CmdResponse)

//HandlerInfo 处理函数信息
type HandlerInfo struct {
	//数据回应的topic
	Topic string
	//协议处理的回调函数
	Handler FunCmdHandler
	//超时时间
	Timeout int64
	//对于回应的处理函数
	ResHandler FunCmdResponseHandler
}
